# -*- makefile -*-

SHELL = /bin/sh

VPATH = $(SRCDIR)

# Set GCCPREFIX to the customized prefix, if any. Leave empty if the prefix is
# i386-elf-
GCCPREFIX=

# Infer the correct GCCPREFIX
ifndef GCCPREFIX
GCCPREFIX := $(shell if i386-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \
  then echo 'i386-elf-'; \
  elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \
  then echo ''; \
  else echo "***" 1>&2; \
  echo "*** Error: Could not find i386-*-elf compiler toolchain." 1>&2; \
  echo "*** Make sure i386-*-elf-gcc is in your PATH" 1>&2; \
  echo "*** If your i386-*-elf toolchain has a prefix" 1>&2; \
  echo "*** other than 'i386-elf-', set the GCCPREFIX" 1>&2; \
  echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
  echo "***" 1>&2; exit 1; fi)
endif

CC  = $(GCCPREFIX)gcc
AS  = $(GCCPREFIX)as
AR  = $(GCCPREFIX)ar
LD  = $(GCCPREFIX)ld
NM  = $(GCCPREFIX)nm
RANLIB = $(GCCPREFIX)ranlib
OBJCOPY = $(GCCPREFIX)objcopy
OBJDUMP = $(GCCPREFIX)objdump

ifeq ($(strip $(shell command -v $(CC) 2> /dev/null)),)
$(warning *** Compiler ($(CC)) not found.  Did you set $$PATH properly?  Please refer to the Getting Started section in the documentation for details. ***)
endif

# Compiler and assembler invocation.
DEFINES =
WARNINGS = -Wall -W -Wstrict-prototypes -Wmissing-prototypes -Wsystem-headers
CFLAGS = -m32 -g -msoft-float -O0
CPPFLAGS = -nostdinc -I$(SRCDIR) -I$(SRCDIR)/lib
ASFLAGS = -Wa,--gstabs,--32
LDFLAGS = 
# LDOPTIONS will be applied directly with 'ld' while LDFLAGS will be applied with 'gcc'.
LDOPTIONS = -melf_i386
DEPS = -MMD -MF $(@:.o=.d)

# Turn off -fstack-protector, which we don't support.
ifeq ($(strip $(shell echo | $(CC) -fno-stack-protector -E - > /dev/null 2>&1; echo $$?)),0)
CFLAGS += -fno-stack-protector
endif

# Turn off --build-id in the linker, which confuses the Pintos loader.
ifeq ($(strip $(shell $(LD) --help | grep -q build-id; echo $$?)),0)
LDFLAGS += -Wl,--build-id=none
endif

PINTOS = $(SRCDIR)/utils/pintos
QEMU = qemu-system-i386
